package com.qupai.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qupai.dto.AuctioneerAddDTO;
import com.qupai.dto.AuctioneerPageQueryDTO;
import com.qupai.entity.Auctioneer;
import com.qupai.mapper.AuctioneerMapper;
import com.qupai.result.PageResult;
import com.qupai.service.IAuctioneerService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 拍卖师表，存储拍卖师信息 服务实现类
 * </p>
 *
 * @author bluefoxyu
 * @since 2024-07-30
 */
@Service
@RequiredArgsConstructor
public class AuctioneerServiceImpl extends ServiceImpl<AuctioneerMapper, Auctioneer> implements IAuctioneerService {

    private final AuctioneerMapper auctioneerMapper;

    @Override
    public void add(AuctioneerAddDTO auctioneerAddDTO) {
        Auctioneer auctioneer=new Auctioneer();
        BeanUtils.copyProperties(auctioneerAddDTO,auctioneer);
        auctioneer.setChance(1);//默认只能发布一次拍卖会
        auctioneerMapper.insert(auctioneer);

    }

    @Override
    public void addChange(Long auctioneerId) {
        Auctioneer auctioneer = auctioneerMapper.selectById(auctioneerId);
        Integer chance = auctioneer.getChance();
        chance=chance+1;
        auctioneer.setChance(chance);
        auctioneerMapper.updateById(auctioneer);
    }

    @Override
    public PageResult pageQuery(AuctioneerPageQueryDTO auctioneerPageQueryDTO) {
        Page<Auctioneer> page = auctioneerPageQueryDTO.toMpPageDefaultSortByCreateTimeDesc();
        Page<Auctioneer> p = lambdaQuery()
                .like(auctioneerPageQueryDTO.getName() != null, Auctioneer::getName, auctioneerPageQueryDTO.getName())
                .eq(auctioneerPageQueryDTO.getStatus() != null, Auctioneer::getStatus, auctioneerPageQueryDTO.getStatus())
                .page(page);
        return new PageResult(p.getTotal(),p.getPages(),p.getRecords());
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Auctioneer auctioneer = Auctioneer.builder()
                .id(id)
                .status(status)
                .build();
        auctioneerMapper.updateById(auctioneer);
    }

}
